import { reactive } from 'vue'
import { UserListParm } from '@/api/user/UserModel'
import { getUserListApi } from '@/api/user/user'
export default function useUserTable () {
  //定义表格绑定的数据
  const tableData = reactive({
    list: []
  })
  const listParm = reactive<UserListParm>({
    deptId: '',
    currentPage: 1,
    pageSize: 10,
    loginName: '',
    total: 0
  })
  // 查询表格
  const getUserList = async () => {
    let res = await getUserListApi(listParm)
    if (res && res.code == 200) {
      //把返回的数据设置到表格绑定的数据里面
      tableData.list = res.data.records
      //设置分页总条数
      listParm.total = res.data.total
    }
  }
  // 树组件的点击调用查询
  const treeClick = async (deptId: number) => {
    listParm.deptId = deptId
    getUserList()
  }

  //页容量改变时触发
  const sizeChange = (size: number) => {
    listParm.pageSize = size
    //重新加载表格
    getUserList()
  }

  //页数改变时触发
  const currentChange = (page: number) => {
    listParm.currentPage = page
    //重新加载表格
    getUserList()
  }
  //搜索
  const searchBtn = () => {
    getUserList()
  }
  //重置
  const resetBtn = () => {
    listParm.loginName = ''
    getUserList()
  }

  return {
    tableData,
    listParm,
    getUserList,
    treeClick,
    sizeChange,
    currentChange,
    searchBtn,
    resetBtn
  }
}
